<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Containers and move semantics</title>
<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
<link rel="up" href="../move.html" title="Chapter 25. Boost.Move">
<link rel="prev" href="movable_only_classes.html" title="Movable but Non-Copyable Types">
<link rel="next" href="construct_forwarding.html" title="Constructor Forwarding">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
<td align="center"><a href="../../../index.html">Home</a></td>
<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="movable_only_classes.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../move.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="construct_forwarding.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="move.move_and_containers"></a><a class="link" href="move_and_containers.html" title="Containers and move semantics">Containers and move semantics</a>
</h2></div></div></div>
<p>
      Movable but non-copyable types can be safely inserted into containers and movable
      and copyable types are more efficiently handled if those containers internally
      use move semantics instead of copy semantics. If the container needs to "change
      the location" of an element internally (e.g. vector reallocation) it will
      move the element instead of copying it. <span class="bold"><strong>Boost.Container</strong></span>
      containers are move-aware so you can write the following:
    </p>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">container</span><span class="special">/</span><span class="identifier">vector</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cassert</span><span class="special">&gt;</span>

<span class="comment">//Remember: 'file_descriptor' is NOT copyable, but it</span>
<span class="comment">//can be returned from functions thanks to move semantics</span>
<span class="identifier">file_descriptor</span> <span class="identifier">create_file_descriptor</span><span class="special">(</span><span class="keyword">const</span> <span class="keyword">char</span> <span class="special">*</span><span class="identifier">filename</span><span class="special">)</span>
<span class="special">{</span>  <span class="keyword">return</span> <span class="identifier">file_descriptor</span><span class="special">(</span><span class="identifier">filename</span><span class="special">);</span>  <span class="special">}</span>

<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
<span class="special">{</span>
   <span class="comment">//Open a file obtaining its descriptor, the temporary</span>
   <span class="comment">//returned from 'create_file_descriptor' is moved to 'fd'.</span>
   <span class="identifier">file_descriptor</span> <span class="identifier">fd</span> <span class="special">=</span> <span class="identifier">create_file_descriptor</span><span class="special">(</span><span class="string">"filename"</span><span class="special">);</span>
   <span class="identifier">assert</span><span class="special">(!</span><span class="identifier">fd</span><span class="special">.</span><span class="identifier">empty</span><span class="special">());</span>

   <span class="comment">//Now move fd into a vector</span>
   <span class="identifier">boost</span><span class="special">::</span><span class="identifier">container</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">file_descriptor</span><span class="special">&gt;</span> <span class="identifier">v</span><span class="special">;</span>
   <span class="identifier">v</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">fd</span><span class="special">));</span>

   <span class="comment">//Check ownership has been transferred</span>
   <span class="identifier">assert</span><span class="special">(</span><span class="identifier">fd</span><span class="special">.</span><span class="identifier">empty</span><span class="special">());</span>
   <span class="identifier">assert</span><span class="special">(!</span><span class="identifier">v</span><span class="special">[</span><span class="number">0</span><span class="special">].</span><span class="identifier">empty</span><span class="special">());</span>

   <span class="comment">//Compilation error if uncommented since file_descriptor is not copyable</span>
   <span class="comment">//and vector copy construction requires value_type's copy constructor:</span>
   <span class="comment">//boost::container::vector&lt;file_descriptor&gt; v2(v);</span>
   <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
    </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2008-2014 Ion Gaztanaga<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="movable_only_classes.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../move.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="construct_forwarding.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
